package com.linsen.core.mybatisplus.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.linsen.core.mybatisplus.constant.EntityConstant;
import com.linsen.core.mybatisplus.constant.GeneratorConstant;
import com.linsen.core.mybatisplus.props.LinsenGeneratorProperties;
import org.springframework.core.io.ClassPathResource;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * MybatisPlus代码生成器
 *
 * @author zhaoyong
 * @date 2023/3/13
 */
public class LinsenFastAutoGenerator {
    public static void main(String[] args) throws IOException {
        Yaml yaml = new Yaml();
        ClassPathResource classPathResource = new ClassPathResource("linsen-mybatisplus-generator.yml");
        LinsenGeneratorProperties linsenGeneratorProperties = yaml.loadAs(classPathResource.getInputStream(), LinsenGeneratorProperties.class);
        List<LinsenGenerateNode> nodes = linsenGeneratorProperties.getNodes();
        for (LinsenGenerateNode node : nodes) {
            FastAutoGenerator.create(
                            node.getJdbc().getUrl(),
                            node.getJdbc().getUser(),
                            node.getJdbc().getPassword()
                    )
                    .globalConfig(builder -> {
                        builder.author("zhaoyong") // 设置作者
                                // .enableSwagger() // 开启 swagger 模式
                                .fileOverride() // 覆盖已生成文件
                                .disableOpenDir()
                                .outputDir(String.format("%s\\%s%s", GeneratorConstant.USER_DIR, node.getId(), GeneratorConstant.JAVA_DIR)); // 指定输出目录
                    })
                    .packageConfig(builder -> {
                        builder.parent(node.getParent()) // 设置父包名
                                .moduleName(node.getModule()) // 设置父包模块名
                                .pathInfo(Collections.singletonMap(OutputFile.mapperXml, String.format("%s\\%s%s\\mapper", GeneratorConstant.USER_DIR, node.getId(), GeneratorConstant.RESOURCES_DIR))); // 设置mapperXml生成路径
                    })
                    .strategyConfig(builder -> {
                        builder.addInclude(node.getTables()) // 设置需要生成的表名
                                .addTablePrefix("t_"); // 设置过滤表前缀
                        builder.controllerBuilder()
                                .enableRestStyle()
                                .enableHyphenStyle();
                        builder.entityBuilder()
                                .enableLombok()
                                .enableTableFieldAnnotation()
                                .enableRemoveIsPrefix()
                                .addTableFills(
                                        new Column(EntityConstant.CREATE_BY_COLUMN, FieldFill.INSERT),
                                        new Column(EntityConstant.CREATE_NAME_BY_COLUMN, FieldFill.INSERT),
                                        new Column(EntityConstant.CREATE_TIME_COLUMN, FieldFill.INSERT),
                                        new Column(EntityConstant.TENANT_ID_COLUMN, FieldFill.INSERT),
                                        new Column(EntityConstant.DELETED_COLUMN, FieldFill.INSERT),
                                        new Column(EntityConstant.UPDATE_BY_COLUMN, FieldFill.INSERT_UPDATE),
                                        new Column(EntityConstant.UPDATE_NAME_BY_COLUMN, FieldFill.INSERT_UPDATE),
                                        new Column(EntityConstant.UPDATE_TIME_COLUMN, FieldFill.INSERT_UPDATE)
                                )
                                // .logicDeleteColumnName(EntityConstant.DELETED_COLUMN)
                                // .logicDeletePropertyName(EntityConstant.DELETED_PROPERTY)
                                .convertFileName(entityName -> entityName.concat("Entity"));
                        builder.mapperBuilder()
                                .enableMapperAnnotation()
                                .enableBaseColumnList()
                                .enableBaseResultMap();
                    })
                    .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                    .execute();
        }

    }
}
